-[UIImage drawInRect:] / CGContextDrawImage() not releasing memory?
        Posted  
        
            by sohocoke
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by sohocoke
        
        
        
        Published on 2010-06-02T21:35:15Z
        Indexed on 
            2010/06/02
            21:54 UTC
        
        
        Read the original article
        Hit count: 1182
        
I wanted to easily blend a UIImage on top of another background image, so wrote a category method for UIImage, adapted from http://stackoverflow.com/questions/1309757/blend-two-uiimages :
- (UIImage *) blendedImageOn:(UIImage *) backgroundImage  {
 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
 UIGraphicsBeginImageContext(backgroundImage.size);
 CGRect rect = CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height);
 [backgroundImage drawInRect:rect];
 [self drawInRect:rect];
 UIImage* blendedImage = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();
 [pool release];
 return blendedImage;
}
Unfortunately my app that uses the above method to load around 20 images and blend them with background and gloss images (so probably around 40 calls), is being jettisoned on the device.
An Instruments session revealed that calls to malloc stemming from the calls to drawInRect: are responsible for the bulk of the memory usage. I tried replacing the drawInRect: messages with equivalent function calls to the function CGContextDrawImage but it didn't help. The AutoReleasePool was added after I found the memory usage problem; it also didn't make a difference.
I'm thinking this is probably because I'm not using graphics contexts appropriately. Would calling the above method in a loop be a bad idea because of the number of contexts I create? Or did I simply miss something?
© Stack Overflow or respective owner